package cn.yyd.user.service.impl;


import cn.yyd.basic.constants.BaseConstants;
import cn.yyd.basic.dto.SmsCodeDto;
import cn.yyd.basic.exception.BusinessException;
import cn.yyd.basic.service.impl.BaseServiceImpl;
import cn.yyd.basic.utils.MD5Utils;
import cn.yyd.basic.utils.StrUtils;
import cn.yyd.user.domain.Logininfo;
import cn.yyd.user.domain.User;
import cn.yyd.user.mapper.LogininfoMapper;
import cn.yyd.user.mapper.UserMapper;
import cn.yyd.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2022-07-06
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;


    /**
    * @Title: registerPhone
    * @Description: 根据手机号注册实现
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/7/6 14:31
    * @Parameters: [smsCodeDto]
    * @Return void
    */
    @Override
    public void registerPhone(SmsCodeDto smsCodeDto) {
        String phone = smsCodeDto.getPhone();
        String phoneCodeValue = smsCodeDto.getPhoneCodeValue();
        String password = smsCodeDto.getPassword();
        String confirmPassword = smsCodeDto.getConfirmPassword();
        // 1.为空校验
        String phoneCodeKey = BaseConstants.VerifyCodeConstant.BUSINESS_REGISTER_KEY + phone;
        Object phoneCodeValueRedis = redisTemplate.opsForValue().get(phoneCodeKey);
        // 2.两次手机号是否一致
        if (phoneCodeValueRedis == null){
            throw new BusinessException("验证码已失效，请重新获取！");
        }
        // 3.判断验证码是否正确
        String phoneCode = phoneCodeValueRedis.toString().split(":")[1];
        if (!phoneCode.equalsIgnoreCase(phoneCodeValue)){
            throw new BusinessException("手机验证码错误，请重新输入！");
        }
        // 4.两次密码是否一致
        if (!password.equals(confirmPassword)){
            throw new BusinessException("两次密码不一致，请重新输入！");
        }
        //5.判断用户是否已注册，如果已注册提示用户
        User user = userMapper.loadByPhone(phone);
        if (user != null){
            throw new BusinessException("用户已注册，请直接登录，如果忘记密码，请点击忘记密码！");
        }
        // 6.初始化user表,就是对你的user对象进行赋值操作
        user = smsCodeDto2User(smsCodeDto);
        // 保存user表
        userMapper.save(user);
        // 7.保存logininfo
        Logininfo logininfo = user2Logininfo(user);
        logininfoMapper.save(logininfo);
        // 8.将logininfo主键设置到user表，并修改
        user.setLogininfoId(logininfo.getId());
        userMapper.update(user);
    }


    /**
    * @Title: smsCodeDto2User
    * @Description: 将smsCodeDto的值赋值给user
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/7/6 14:44
    * @Parameters: [smsCodeDto]
    * @Return cn.itsource.user.domain.User
    */
    private User smsCodeDto2User(SmsCodeDto smsCodeDto) {
        String phone = smsCodeDto.getPhone();
        User user = new User();
        user.setUsername(phone);
        user.setPhone(phone);
        String salt = StrUtils.getComplexRandomString(32);
        String saltPassword = MD5Utils.encrypByMd5(salt + smsCodeDto.getPassword());
        user.setPassword(saltPassword);
        user.setSalt(salt);
        return user;
    }

    /**
    * @Title: user2Logininfo
    * @Description: 将user的值赋值给logininfo
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/7/6 14:44
    * @Parameters: [user]
    * @Return cn.itsource.user.domain.Logininfo
    */
    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user, logininfo);
        logininfo.setType(1);
        return logininfo;
    }

}
